/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManageJFrame;

import java.awt.*;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;
/**
 *
 * @author Pham
 */
public class JFrameMain extends JFrame{
    JPanel pnbasic;
    JMenuBar mnbar;
    JMenu menuSystem;
    public static JMenuItem menuItemLogin;
    public static JMenuItem menuItemLogout;
    JMenuItem menuItemChangePass;
    JMenuItem menuExit;
    public static JMenu menuOptions;
    JMenuItem menuItemProduct;
    JMenuItem menuItemUer;
    JMenuItem menuItemOrder;
    JMenuItem menuItemImport;
    JMenuItem menuItemExport;
    JMenuItem menuItemIntentory;
    JMenu menuHelp;
    public static JTabbedPane tabpn;
    
   
    
    public JFrameMain()
    {
        initComponents();
    }

    private void initComponents() {
        pnbasic = new JPanel();
        //pnbasic.setLayout(new BoxLayout(pnbasic, BoxLayout.Y_AXIS));
        this.add(pnbasic);
        
        //add menubar
        mnbar = new JMenuBar();
        
        this.setJMenuBar(mnbar); //set menubar for main form.
        //add first menu
        menuSystem = new JMenu("Hệ thống");
        menuSystem.setMnemonic(KeyEvent.VK_H);
        mnbar.add(menuSystem);
        
        //add the menuItems to menuSystem
        menuItemLogin = new JMenuItem("Đăng nhập");
        ImageIcon iconLogin = new ImageIcon(new ImageIcon(JFrameMain.class.getResource("../images/login.png"))
                .getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH));
        menuItemLogin.setIcon(iconLogin);
        menuItemLogin.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                menuItemLoginactionPerformed(e);
            }
        });
        menuSystem.add(menuItemLogin);
        
        menuItemLogout = new JMenuItem("Đăng xuất");
        ImageIcon iconLogout = new ImageIcon(new ImageIcon(JFrameMain.class.getResource("../Images/logout.png"))
                .getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH));
        menuItemLogout.setIcon(iconLogout);
        menuItemLogout.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                menuItemLogoutactionPerformed(e);
            }
            
        });
        menuSystem.add(menuItemLogout);
            //add separatior
        menuSystem.addSeparator();
        
        menuItemChangePass = new JMenuItem("Đổi mật khẩu");
        ImageIcon iconChangePass = new ImageIcon(new ImageIcon(JFrameMain.class.getResource("../Images/key.png"))
                .getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH));
        menuItemChangePass.setIcon(iconChangePass);
        menuSystem.add(menuItemChangePass);
        
        menuSystem.addSeparator();
        
        menuExit = new JMenuItem("Tắt chương trình");
        ImageIcon iconExit = new ImageIcon(new ImageIcon(JFrameMain.class.getResource("../Images/exit.png"))
                .getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH));
        menuExit.setIcon(iconExit);
        menuExit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                menuExitactionPerformed(e);
            }
        });
        menuSystem.add(menuExit);
        //------------------------------
        //add second menu
        menuOptions = new JMenu("Quản lý");
        menuOptions.setMnemonic(KeyEvent.VK_Q);
        mnbar.add(menuOptions);
        
        //add menuItems to menuOptions
        menuItemProduct = new JMenuItem("Quản lý sản phẩm");
        ImageIcon iconProduct = new ImageIcon(new ImageIcon(JFrameMain.class.getResource("../Images/laptop.png"))
                .getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH));
        menuItemProduct.setIcon(iconProduct);
        menuItemProduct.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                menuItemProductactionPerformed(e);
            }
        });
        
        menuItemUer = new JMenuItem("Quản lý người dùng");
        ImageIcon iconUser = new ImageIcon(new ImageIcon(JFrameMain.class.getResource("../Images/man_brown.png"))
                .getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH));
        menuItemUer.setIcon(iconUser);
        
        menuItemOrder = new JMenuItem("Quản lý đơn hàng");
        ImageIcon iconOrder = new ImageIcon(new ImageIcon(JFrameMain.class.getResource("../Images/order.png"))
                .getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH));
        menuItemOrder.setIcon(iconOrder);
        
        menuItemImport = new JMenuItem("Quản lý nhập hàng");
        ImageIcon iconImport = new ImageIcon(new ImageIcon(JFrameMain.class.getResource("../Images/import.png"))
                .getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH));
        menuItemImport.setIcon(iconImport);
        
        menuItemExport = new JMenuItem("Quản lý xuất hàng");
        ImageIcon iconExport = new ImageIcon(new ImageIcon(JFrameMain.class.getResource("../Images/export.png"))
                .getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH));
        menuItemExport.setIcon(iconExport);
        
        menuItemIntentory = new JMenuItem("Quản lý kho hàng");
        ImageIcon iconInventory = new ImageIcon(new ImageIcon(JFrameMain.class.getResource("../Images/box1.png"))
                .getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH));
        menuItemIntentory.setIcon(iconInventory);
        
        menuOptions.add(menuItemProduct);
        menuOptions.add(menuItemUer);
        menuOptions.add(menuItemOrder);
        menuOptions.add(menuItemImport);
        menuOptions.add(menuItemExport);
        menuOptions.add(menuItemIntentory);
        
        //------------------------------------------
        //add menuHelp to menubar
        menuHelp = new JMenu("Trợ giúp");
        menuHelp.setMnemonic(KeyEvent.VK_T);
        
        mnbar.add(menuHelp);
        
        //add tabpanel
        tabpn = new JTabbedPane();
//        tabpn.addTab("tab1", (new JFrameManageProduct()).getContentPane());
//        tabpn.addTab("tab2", (new JFrameLogin()).getContentPane());
        add(tabpn);
        
        //disable menuItem if user hasn't logged
        menuItemLogout.setEnabled(false);
        menuItemChangePass.setEnabled(false);
        menuOptions.setEnabled(false);
        
        
    }
    
    /////////action method//////////
    //method action menuItem product
    private void menuItemProductactionPerformed(ActionEvent e) {
        
            tabpn.add(menuItemProduct.getText(), (new JFrameManageProduct()).getContentPane());
    }
    
    //exit action
    private void menuExitactionPerformed(ActionEvent e) {
        int mess = JOptionPane.showConfirmDialog(this, "Bạn có chắc muốn thoát?", "Thông báo", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if(mess == JOptionPane.YES_OPTION)
        {
            System.exit(0);
        }
        
    }
    
    //method action of menuItemLogin
    private void menuItemLoginactionPerformed(ActionEvent e) {
        ShowLoginFrame();
    }
    
    //method action menuItemlogout
    private void menuItemLogoutactionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(this, "Đã đăng xuất");
        menuItemLogin.setEnabled(true);
        menuOptions.setEnabled(false);
        menuItemLogout.setEnabled(false);
    }
    
    //method create framelogin
    private static void ShowLoginFrame()
    {
        JFrameLogin framelogin = new JFrameLogin();
        //pnbasic.add(framelogin);
        
        
        framelogin.setPreferredSize(new Dimension(450, 300));
        framelogin.pack();
        
        //framelogin.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        framelogin.setLocation(dim.width/2 - framelogin.getWidth()/2, dim.height/2 - framelogin.getHeight()/2);
       
        framelogin.setVisible(true);
    }
    
    //method add tab
    private void addNewTab(Component pane)
    {
        
    }
    
    //create and showUI
    public static void CreateAndShowUI()
    {
        JFrameMain frameMain = new JFrameMain();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        frameMain.setTitle("Phần mềm quản lý cửa hàng laptop LHL");
        frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frameMain.setPreferredSize(new Dimension(820, 500));
        //frameMain.setPreferredSize(new Dimension(dim.width, (int)(dim.height - dim.height*0.1)));
        frameMain.pack();
        
        
        int x = (dim.width - frameMain.getWidth())/2;
        int y = (dim.height - frameMain.getHeight())/2;
        frameMain.setLocation(x, y);
        
        frameMain.setVisible(true);
        
        //Show login frame when application run
        ShowLoginFrame();
    }
    
    
    //main method
    public static void main(String[] args)
    {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JDialogProduct.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JDialogProduct.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JDialogProduct.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JDialogProduct.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        
        /*Create and display Main*/
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                CreateAndShowUI();
            }
        });
    }
}
